package com.example.utils;

import com.example.common.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PathUils {

    public static void main(String[] args) throws Exception {
        String path1=new File(".").getCanonicalPath();
        System.out.println(path1);
        File file4 = new File(PathUils.class.getResource("/").getPath());
        String path = file4.getAbsolutePath();
        System.out.println(path);
    }

    public static String getPath(){
        String path="";
        try {
            path=new File(PathUils.class.getResource("/").getPath()).getAbsolutePath();
        }catch (Exception e){
            e.printStackTrace();
        }
        return path ;
    }

    //获取路径列表文件夹
    public static String[] getSystemFileList(File file){
        return file.list();
    }


    /**
     * 获取上传文件路径
     * @param filename
     * @return
     */
    public static String getUploadPath(String filename){
        String dateString= DateUtils.getStringByDate(new SimpleDateFormat("yyyy-MM"),new Date());
        return getCommonPath(Constants.UPLOAD_FILE,dateString,filename);
    }



    /**
     * 获取索引路径
     * @param index
     * @return
     */
    public static String getIndexPath(String index){
        return getCommonPath(Constants.INDEX_PATH,null,index);
    }
    /**
     * 获取序列化的路径
     * @param path
     * @return
     */
    public static String getSerializePath(String path){
       return getCommonPath(Constants.SERIALIZE_PATH,String.valueOf(System.currentTimeMillis()),path);
    }


    /**
     * 获取公共路径
     * @param contantsPath
     * @param datePath
     * @param name
     * @return
     */
    private static String getCommonPath(String contantsPath,String datePath,String name){
        String path=getPath();
        if(StringUtils.isNotBlank(contantsPath)){
            path+=File.separator+contantsPath;
        }
        if(StringUtils.isNotBlank(datePath)){
            path+=File.separator+datePath;
        }
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return path+File.separator+name;
    }

}
